




/* 

Name: table_OA6.do 
Created: June 2022 
Aim: look at RIS and the share of intra firm trade 

Inputs: 

- intra_frm_us_2002.csv
from https://www.census.gov/programs-surveys/trade/data/tables/relatedparty.html 

- hs_sic_naics_exports_89_109_20111004
from http://faculty.som.yale.edu/peterschott/files/research/data/appendix_files_20111004.zip 

Output: table_online_6.xls

*/ 

cd $sectorpath 

*** build data 
use hs_sic_naics_exports_89_109_20111004, clear
keep if year==102 
g hs6=substr(commodity,1,6) 
duplicates drop hs6 , force 
keep naics hs6 
sort hs6 
save temp_naicshs6, replace

import delimited $countrypath\intra_firm_us_2002.csv, delimiter(comma) clear
g naics6=substr(naics, 1,6) 
g shx_intra=exp_rel/(exp_rel+exp_non)
g shm_intra=imp_rel/(imp_rel+imp_non)
sort naics6 
save temp, replace

use $rspath/rs_hs02_19962006 , clear
g hs6=hs6_2002
sort hs6 	
merge 1:1 hs6 using temp_naicshs6
keep if _m==3 
drop _m 
sort hs6 
merge 1:m hs6_2002 using hs2002_to_sitc2.dta 
drop if _m==2 
drop _m

// 2- merge with several product characteristics 

sort sitc2_4d 
merge m:1 sitc2_4d using rauch_sitc2_4digit.dta // Rauch measure
drop if _m==2 
rename _m _mm
rename lib lib_34 
rename con con_34 
sort sitc2_3d 
merge m:1 sitc2_3d using rauch_sitc2_3digit.dta // Rauch measure
replace lib_34 = lib if _mm==1 
replace con_34 = con if _mm==1 
drop if _merge==2
drop _m* 

duplicates drop hs6, force 
sort hs6 
merge 1:1 hs6 using isic.dta 
drop if _m==2 
drop _m 

tostring i2code, replace 
g industry_code=substr(i2code,1,3)
sort industry_code
merge m:1 industry_code using contract_intensity_ISIC_1997.dta // Nunn' measure of relationship specificity 
drop _m 

sort hs6 
merge 1:1 hs6 using upstreamness_hs6.dta // Antras et al. measure of upstreamness
drop if _m==2 
drop _m 

g hs4 = substr(hs6,1,4) 
g hs2 = substr(hs6,1,2) 

sort hs4 
merge m:1 hs4 using sigma_hs4_imbsmejean.dta // Imbs & Mejean measure of elasticity 
drop if _m==2 
drop _m 


collapse (mean) rs_hs6_b  frac_lib_diff frac_lib_not_homog lib_org_exch lib_diff upstreamness sigma, by(naics) // taking the median does not change the results 
rename naics naics6 
merge 1:1 naics6 using temp 
keep if _m==3 

g ris = rs


*** regressions 
reg shm ris , ro
outreg2 using $outputpath/table_online_6.xls, replace ctitle(imports) dec(3)
reg shm ris frac_lib_diff upstreamness sigma , ro
outreg2 using $outputpath/table_online_6.xls, append ctitle(imports) dec(3)
reg shx ris, ro
outreg2 using $outputpath/table_online_6.xls, append ctitle(exports) dec(3)
reg shx ris frac_lib_diff upstreamness sigma, ro
outreg2 using $outputpath/table_online_6.xls, append ctitle(exports) dec(3)

erase temp_naicshs6.dta 
erase temp.dta 
